//
//  GHManyPic.m
//  VideoPic
//
//  Created by GH on 16/3/30.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "GHManyPic.h"
#import "GHPicGroupModel.h"
#import <YYWebImage.h>
#import "HomeViewController.h"
#import "GHPushLargeVC.h"




#define CELL_WIDTH self.bounds.size.width


@interface GHManyPic ()<UIGestureRecognizerDelegate>
@property (nonatomic ,retain)UILabel *labelOfUserName;
@property (nonatomic ,retain)UILabel *labelContect;
@property (nonatomic ,retain)UILabel * labelOfCap;
@property (nonatomic ,retain)UIImageView *imageOfHead;
@property (nonatomic ,retain)GHPicGroupModel *model;
@property (nonatomic ,strong)NSMutableArray *arrWidth;
@property (nonatomic ,strong)NSMutableArray *arrHeight;


@end

@implementation GHManyPic


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createImage];
        [self createLabel];
    }
    
    return self;
}

- (void)createImage {
    
    
    self.imageOfHead = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imageOfHead];
    
    for (int i = 0; i < 9; i++) {
        UIImageView * image = [YYAnimatedImageView new];
            [self.contentView addSubview:image];
        
            image.tag = 10  + i;
        image.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleClick:)];
        
        [image addGestureRecognizer:tapGesture];
        tapGesture.numberOfTouchesRequired = 1;
        tapGesture.delegate = self;
        
    }
    
    
}
- (void)handleClick:(UITapGestureRecognizer *)tap {
    
    id result = [self nextResponder];
    while (![result isKindOfClass:[HomeViewController class]] && result != nil) {
        result = [result nextResponder];
    }
    HomeViewController *vc =  (HomeViewController *)result;
    
    GHPushLargeVC * push = [[GHPushLargeVC alloc] init];
    
    UIImageView *image = [self.contentView viewWithTag:tap.view.tag];
    
    CGRect frame = [image convertRect:[UIScreen mainScreen].bounds toView:0];
    
    push.x = frame.origin.x;
    push.y = frame.origin.y;
    push.url = self.model.arrPic[tap.view.tag - 10];
    push.height = self.model.width_large.integerValue;
    push.heightNew = self.model.height_large.integerValue;
    push.arrUrl = self.model.arrPic;
    push.arrHeight = self.arrHeight;
    push.arrWidth = self.arrWidth;
  
//    vc.hidesBottomBarWhenPushed = YES;
//    vc.navigationController.navigationBar.hidden = YES;
    
    [vc.navigationController pushViewController:push animated:NO];
    
    
    
    

}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    
    return YES;
}


- (void)createLabel {
    
    self.labelOfUserName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfUserName];
    
    self.labelContect = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelContect];
    self.labelContect.numberOfLines = 0;
    
    self.labelOfCap = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfCap];
    self.labelOfCap.font = [UIFont systemFontOfSize:12];

}
- (void)passModelWith:(GHPicGroupModel *)model{
    
    self.model = model;
    
    self.model.r_height = @([UIScreen mainScreen].bounds.size.width *self.model.r_height.floatValue / self.model.r_width.floatValue);
    
    self.model = model;
    self.labelOfUserName.text = model.name;
    self.imageOfHead.yy_imageURL = [NSURL URLWithString:model.avatar_url];
    self.labelContect.text = model.content;
    self.labelContect.numberOfLines = 0;
    self.labelOfCap.text = model.category_name;
    self.arrHeight = [NSMutableArray array];
    self.arrWidth = [NSMutableArray array];

    for (int i = 0; i < model.arrPic.count; i++) {
        UIImageView *image = [self.contentView viewWithTag:10 + i];
        image.yy_imageURL = [NSURL URLWithString:model.arrPic[i]];
        [self.arrWidth addObject:model.width_large];
        [self.arrHeight addObject:model.height_large];
        
    }
    
}




-(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(CELL_WIDTH - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}



- (void)layoutSubviews {
    [super layoutSubviews];
    NSLog(@"%@",self.model.media_type);

    //头像
    self.imageOfHead.frame = CGRectMake(20, 20, CELL_WIDTH / 10, CELL_WIDTH / 10);
    self.imageOfHead.layer.masksToBounds = YES;
    self.imageOfHead.layer.cornerRadius = CELL_WIDTH / 20;
    
    //用户名
    self.labelOfUserName.frame = CGRectMake(30 + CELL_WIDTH / 10, 20 + CELL_WIDTH/ 20 - 13, CELL_WIDTH/2, 25);
    self.labelOfUserName.font = [UIFont systemFontOfSize:13];
    
    //title
    self.labelContect.frame = CGRectMake(20, 20 + CELL_WIDTH /10 +15, CELL_WIDTH  - 40, [self heightForLabel:self.labelContect.text]);
    CGFloat titleBom = [self heightForLabel:self.labelContect.text] + 35 + CELL_WIDTH / 10;
    
    CGFloat userHeight;
    
    //cap
    self.labelOfCap.numberOfLines = 0;
    if (self.labelContect == nil) {
        self.labelOfCap.frame = CGRectMake(20, titleBom + 15 - 20, [self heightForLabel:self.labelOfCap.text], 20);
        userHeight = titleBom + 25;
    }else {
        self.labelOfCap.frame = CGRectMake(20, titleBom + 15, 100, 20);
        userHeight = titleBom + 40;
    }
    self.labelOfCap.textAlignment = 1;
    self.labelOfCap.layer.borderWidth = 1;
    self.labelOfCap.layer.borderColor = [UIColor blackColor].CGColor;
    self.labelOfCap.layer.masksToBounds = YES;
    self.labelOfCap.layer.cornerRadius = 10;
    
    for (int i = 0; i < 9; i ++) {
        
        UIImageView *image = [self.contentView viewWithTag:10 + i];
//        NSLog(@"%@",[self.contentView viewWithTag:10 + i]);
        if (i < 3) {
            image.frame = CGRectMake(10 + i *(CELL_WIDTH )/3 , userHeight, (CELL_WIDTH - 60) / 3 , (CELL_WIDTH - 60) / 3);
        }else if (i < 6){
            image.frame = CGRectMake(10 + (i -3) *(CELL_WIDTH )/3 , userHeight + (CELL_WIDTH - 60) / 3 + 10, (CELL_WIDTH - 60) / 3 , (CELL_WIDTH - 60) / 3);
        }else {
         image.frame = CGRectMake(10 + (i -6) *(CELL_WIDTH )/3 , userHeight + (CELL_WIDTH - 60) / 3 *2 + 20, (CELL_WIDTH - 60) / 3 , (CELL_WIDTH - 60) / 3);
        
        }
    
    }
    

}

- (void)prepareForReuse{
    [super prepareForReuse];
//    for (int i = 0; i < 9; i ++) {
//        UIImageView *image = [self.contentView viewWithTag:10 + i];
//        [image removeFromSuperview];
//    }
}
//
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
